package Game;

import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.Calendar;

import javax.imageio.ImageIO;

public class Ast {

    private static final int diametro = 50;
    int x = 0, y = 400, ya = 0, xcol = 0;
    int res = 400;
    int y1, y2, y3, y4, y5;
    private final Game g;
    private Image img;
    private int posale1 = aleatorioAst(2, false, 1);
    private int posale2 = aleatorioAst(2, true, 2);
    private int posale3 = aleatorioAst(2, true, 3);
    private int posale4 = aleatorioAst(2, true, 4);
    private int posale5 = aleatorioAst(2, true, 5);
    int margenerror = 15;
    int margenerror2 = margenerror * 2;
    int score = 0;
    int scoreve = 2;
    Calendar tiempo = Calendar.getInstance();
    static Calendar tiempoRes = Calendar.getInstance();
    static boolean empezar = true;
    boolean choque = false;
    private int x11, x12, yx1, x21, x22, yx2, x31, x32, yx3, x41, x42, yx4,
            x51, x52, yx5;

    public void reset() {
        xcol = 0;
        x = 0;
        y = res;
        ya = 0;
        scoreve = 2;
        tiempo = Calendar.getInstance();
        g.resNav();
        choque = false;
    }

    public void resetColisiones() {
        xcol = 600;
        choque = true;
    }

    public void setScore() {
        score = 0;
    }

    public Ast(Game gm) {
        this.g = gm;
        ast(1);
        ast(2);
        ast(3);
    }

    public int getScore() {
        return this.score;
    }

    private int aleatorioAst(int ncasos, boolean pos, int numpos) {
        int ale;
        ale = (int) Math.round(Math.random() * ncasos) + 1;
        if (pos) {
            ale = posAleRe(ncasos, pos, numpos, ale);
        }
        return ale;
    }

    private int posAleRe(int ncasos, boolean pos, int numpos, int ale) {
        boolean listo = false;
        while (listo == false) {
            switch (numpos) {
                case 2:
                    if (posale1 == ale) {
                        ale = (int) Math.round(Math.random() * ncasos) + 1;
                    } else {
                        listo = true;
                    }
                    break;
                case 3:
                    if (posale2 == ale) {
                        ale = (int) Math.round(Math.random() * ncasos) + 1;
                    } else {
                        listo = true;
                    }
                    break;
                case 4:
                    if (posale3 == ale) {
                        ale = (int) Math.round(Math.random() * ncasos) + 1;
                    } else {
                        listo = true;
                    }
                    break;
                case 5:
                    if (posale4 == ale) {
                        ale = (int) Math.round(Math.random() * ncasos) + 1;
                    } else {
                        listo = true;
                    }
                    break;
                default:
                    ale = (int) Math.round(Math.random() * ncasos) + 1;
                    break;
            }
        }
        return ale;
    }

    private void ast(int pos) {
        String imagen = "images/asteroide.png";
        URL imgUrl = getClass().getClassLoader().getResource(imagen);
        if (imgUrl == null) {
            System.err.println("Couldn't find file: " + imagen);
        } else {
            try {
                imagenA(imgUrl, pos);
            } catch (IOException ex) {
            }
        }
    }

    private void imagenA(URL imgUrl, int pos) throws IOException {
        if (pos == 1) {
            img = ImageIO.read(imgUrl);
        }
    }

    public void mover() {
        Calendar timac = Calendar.getInstance();
        if (((timac.getTimeInMillis() - tiempo.getTimeInMillis()) / 1000) == 1) {
            ya = 2;
        }
        if (((timac.getTimeInMillis() - tiempoRes.getTimeInMillis()) / 1000) == 2
                && empezar == false) {
            reset();
        }
        y = y + ya;
        if (collision11()) {
            this.g.gameOver();
        }
        if (collision12()) {
            this.g.gameOver();
        }
        if (collision21()) {
            this.g.gameOver();
        }
        if (collision22()) {
            this.g.gameOver();
        }
        if (collision31()) {
            this.g.gameOver();
        }
        if (collision32()) {
            this.g.gameOver();
        }
        if (collision41()) {
            this.g.gameOver();
        }
        if (collision42()) {
            this.g.gameOver();
        }
        if (collision51()) {
            this.g.gameOver();
        }
        if (collision52()) {
            this.g.gameOver();
        }
    }

    public void pintar(Graphics2D g) {
        int espacio = 3;
        repeticion(g, espacio, posale1, 1);
        espacio += 3;
        repeticion(g, espacio, posale2, 2);
        espacio += 3;
        repeticion(g, espacio, posale3, 3);
        espacio += 3;
        repeticion(g, espacio, posale4, 4);
        espacio += 3;
        repeticion(g, espacio, posale5, 5);
    }

    private void repeticion(Graphics2D g, int d, int posalea, int posreal) {
        int med = diametro * d;
        if (y >= med) {
            aster(g, med, posalea, posreal);
        }
    }

    public void aster(Graphics2D g, int med, int posalea, int posreal) {
        switch (posalea) {
            case 1:
                asteroide(g, med, "iz", posreal);
                asteroide(g, med, "medio", posreal);
                break;
            case 2:
                asteroide(g, med, "iz", posreal);
                asteroide(g, med, "derecha", posreal);
                break;
            default:
                asteroide(g, med, "medio", posreal);
                asteroide(g, med, "derecha", posreal);
                break;
        }
    }

    private void asteroide(Graphics2D g, int med, String posicion, int ynueva) {
        med += diametro;
        int ypint = y;
        int realesp = this.g.getHeight() + (diametro * 3) + 50;
        switch (ynueva) {
            case 1:
                if (ypint + y1 - diametro - med >= this.g.getHeight()) {
                    y1 = y1 - realesp;
                    posale1 = aleatorioAst(2, false, 1);
                    masuno();
                    if (ya <= 7) {
                        ya += 2;
                        scoreve += 2;
                    }
                }
                ypint += y1;
                switch (posale1) {
                    case 1:
                        x11 = xcol + diametro;
                        x12 = xcol;
                        break;
                    case 2:
                        x11 = xcol;
                        x12 = xcol + (diametro * 2);
                        break;
                    default:
                        x11 = xcol + diametro;
                        x12 = xcol + (diametro * 2);
                        break;
                }
                yx1 = ypint - med;
                break;
            case 2:
                if (ypint + y2 - diametro - med >= this.g.getHeight()) {
                    y2 = y2 - realesp;
                    posale2 = aleatorioAst(2, true, 2);
                    masuno();
                }
                ypint += y2;
                switch (posale2) {
                    case 1:
                        x21 = xcol + diametro;
                        x22 = xcol;
                        break;
                    case 2:
                        x21 = xcol;
                        x22 = xcol + (diametro * 2);
                        break;
                    default:
                        x21 = xcol + diametro;
                        x22 = xcol + (diametro * 2);
                        break;
                }
                yx2 = ypint - med;
                break;
            case 3:
                if (ypint + y3 - diametro - med >= this.g.getHeight()) {
                    y3 = y3 - realesp;
                    posale3 = aleatorioAst(2, true, 3);
                    masuno();
                }
                ypint += y3;
                switch (posale3) {
                    case 1:
                        x31 = xcol + diametro;
                        x32 = xcol;
                        break;
                    case 2:
                        x31 = xcol;
                        x32 = xcol + (diametro * 2);
                        break;
                    default:
                        x31 = xcol + diametro;
                        x32 = xcol + (diametro * 2);
                        break;
                }
                yx3 = ypint - med;
                break;
            case 4:
                if (ypint + y4 - diametro - med >= this.g.getHeight()) {
                    y4 = y4 - realesp;
                    posale4 = aleatorioAst(2, true, 4);
                    masuno();
                }
                ypint += y4;
                switch (posale4) {
                    case 1:
                        x41 = xcol + diametro;
                        x42 = xcol;
                        break;
                    case 2:
                        x41 = xcol;
                        x42 = xcol + (diametro * 2);
                        break;
                    default:
                        x41 = xcol + diametro;
                        x42 = xcol + (diametro * 2);
                        break;
                }
                yx4 = ypint - med;
                break;
            case 5:
                if (ypint + y5 - diametro - med >= this.g.getHeight()) {
                    y5 = y5 - realesp;
                    posale5 = aleatorioAst(2, true, 5);
                    masuno();
                }
                ypint += y5;
                switch (posale5) {
                    case 1:
                        x51 = xcol + diametro;
                        x52 = xcol;
                        break;
                    case 2:
                        x51 = xcol;
                        x52 = xcol + (diametro * 2);
                        break;
                    default:
                        x51 = xcol + diametro;
                        x52 = xcol + (diametro * 2);
                        break;
                }
                yx5 = ypint - med;
                break;
            default:
                break;
        }
        switch (posicion) {
            case "derecha":
                if (ypint - med <= this.g.getHeight() + diametro) {
                    g.drawImage(img, x + (diametro * 2), ypint - med, diametro,
                            diametro, null);
                }
                break;
            case "medio":
                if (ypint - med <= this.g.getHeight() + diametro) {
                    g.drawImage(img, x + diametro, ypint - med, diametro, diametro,
                            null);
                }
                break;
            default:
                if (ypint - med <= this.g.getHeight() + diametro) {
                    g.drawImage(img, x, ypint - med, diametro, diametro, null);
                }
                break;
        }
    }

    private void masuno() {
        if (choque == false) {
            score += scoreve;
        }
        res += 155;
        if (Game.colorito < 254) {
            Game.colorito++;
        }
    }

    /*
     * private void vercolisiones(Graphics2D g) { g.drawRect(x11 + margenerror,
     * yx1 + margenerror, diametro - margenerror2, diametro - margenerror2);
     * g.drawRect(x12 + margenerror, yx1 + margenerror, diametro - margenerror2,
     * diametro - margenerror2); g.drawRect(x21 + margenerror, yx2 +
     * margenerror, diametro - margenerror2, diametro - margenerror2);
     * g.drawRect(x22 + margenerror, yx2 + margenerror, diametro - margenerror2,
     * diametro - margenerror2); g.drawRect(x31 + margenerror, yx3 +
     * margenerror, diametro - margenerror2, diametro - margenerror2);
     * g.drawRect(x32 + margenerror, yx3 + margenerror, diametro - margenerror2,
     * diametro - margenerror2); g.drawRect(x41 + margenerror, yx4 +
     * margenerror, diametro - margenerror2, diametro - margenerror2);
     * g.drawRect(x42 + margenerror, yx4 + margenerror, diametro - margenerror2,
     * diametro - margenerror2); g.drawRect(x51 + margenerror, yx5 +
     * margenerror, diametro - margenerror2, diametro - margenerror2);
     * g.drawRect(x52 + margenerror, yx5 + margenerror, diametro - margenerror2,
     * diametro - margenerror2); }
     */
    private boolean collision11() {
        return g.t.getBounds().intersects(getBounds11());
    }

    private boolean collision12() {
        return g.t.getBounds().intersects(getBounds12());
    }

    public Rectangle getBounds11() {
        return new Rectangle(x11 + margenerror, yx1 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    public Rectangle getBounds12() {
        return new Rectangle(x12 + margenerror, yx1 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    private boolean collision21() {
        return g.t.getBounds().intersects(getBounds21());
    }

    private boolean collision22() {
        return g.t.getBounds().intersects(getBounds22());
    }

    public Rectangle getBounds21() {
        return new Rectangle(x21 + margenerror, yx2 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    public Rectangle getBounds22() {
        return new Rectangle(x22 + margenerror, yx2 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    private boolean collision31() {
        return g.t.getBounds().intersects(getBounds31());
    }

    private boolean collision32() {
        return g.t.getBounds().intersects(getBounds32());
    }

    public Rectangle getBounds31() {
        return new Rectangle(x31 + margenerror, yx3 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    public Rectangle getBounds32() {
        return new Rectangle(x32 + margenerror, yx3 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    private boolean collision41() {
        return g.t.getBounds().intersects(getBounds41());
    }

    private boolean collision42() {
        return g.t.getBounds().intersects(getBounds42());
    }

    public Rectangle getBounds41() {
        return new Rectangle(x41 + margenerror, yx4 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    public Rectangle getBounds42() {
        return new Rectangle(x42 + margenerror, yx4 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    private boolean collision51() {
        return g.t.getBounds().intersects(getBounds51());
    }

    private boolean collision52() {
        return g.t.getBounds().intersects(getBounds52());
    }

    public Rectangle getBounds51() {
        return new Rectangle(x51 + margenerror, yx5 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }

    public Rectangle getBounds52() {
        return new Rectangle(x52 + margenerror, yx5 + margenerror, diametro
                - margenerror2, diametro - margenerror2);
    }
}
